//
//  ShouDongGoodDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/12.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ShouDongGoodDetailVC.h"
#import "ScanGoodDetailModel.h"
#import "BillAddPackageGoodModel.h"
#import "ScanGoodDetailInfoCell.h"
#import "ShouGongGoodDetailListCell.h"

@interface ShouDongGoodDetailVC ()<UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <ScanGoodDetailModel *>*dataArray;
@property (nonatomic, strong) BillAddPackageGoodModel *model;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation ShouDongGoodDetailVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"保存" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

- (void)sureClick {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.model.already_jnum forKey:@"already_jnum"];
    [dic setObject:self.model.already_num forKey:@"already_num"];
    [dic setObject:self.model.pnum forKey:@"jnum"];
    [dic setObject:self.model.num forKey:@"num"];
    NSMutableArray *mut = [NSMutableArray array];
    [self.dataArray enumerateObjectsUsingBlock:^(ScanGoodDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dd = [NSMutableDictionary dictionary];
        [dd setObject:obj.Id forKey:@"id"];
        [dd setObject:obj.num_sn forKey:@"num_sn"];
        [dd setObject:obj.num forKey:@"num"];
        [dd setObject:@(obj.is_bind) forKey:@"is_bind"];
        [dd setObject:self.wa_id forKey:@"wa_id"];
        [dd setObject:self.g_id forKey:@"g_id"];
        [mut addObject:dd];
    }];
    [dic setObject:[FactoryMethod toJSONString:mut] forKey:@"listmodel"];
    [HttpRequest PostWithApi:ReceiptDeliveryBillAddPackageShouGongGoodDetailCommit params:dic progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshShouGongAddGoodList object:nil];
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已保存" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSMutableArray<ScanGoodDetailModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"已扫码货物清单";
    page = 1;
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:ReceiptDeliveryBillAddPackageShouGongGoodDetail params:@{@"wa_id":self.wa_id,@"g_id":self.g_id,@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [BillAddPackageGoodModel mj_objectWithKeyValues:responseObject[@"waybillInfo"]];
            if (self->page == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [ScanGoodDetailModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
            }else {
                NSArray *array = [ScanGoodDetailModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.dataArray addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanGoodDetailInfoCell *cell = [ScanGoodDetailInfoCell ScanGoodDetailInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else {
        ShouGongGoodDetailListCell *cell = [ShouGongGoodDetailListCell ShouGongGoodDetailListCellWithTableView:tableView];
        ScanGoodDetailModel *model = self.dataArray[indexPath.row];
        cell.model = model;
        return cell;
    }
}

//- (void)requestForJieChu:(NSString *)num_sn {
//    [HttpRequest PostWithApi:ReceiptDeliveryBillAddPackageScanGoodDetailJieBang params:@{@"wa_id":self.wa_id,@"g_id":self.model.g_id,@"num_sn":num_sn} progress:^(NSProgress * _Nonnull progress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
//        if (code) {
//            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已解除"];
//            self->page = 1;
//            [self requestForList];
//            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshScanAddGoodList object:nil];
//        }
//    } fail:^(NSError * _Nonnull error) {
//
//    }];
//}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanGoodDetailInfoCell *cell = [ScanGoodDetailInfoCell ScanGoodDetailInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else {
        ShouGongGoodDetailListCell *cell = [ShouGongGoodDetailListCell ShouGongGoodDetailListCellWithTableView:tableView];
        ScanGoodDetailModel *model = self.dataArray[indexPath.row];
        cell.model = model;
        return cell.height;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        ScanGoodDetailModel *model = self.dataArray[indexPath.row];
        model.is_bind = model.is_bind == 1 ? 2 : 1;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
